Results 1 to 11 of 11

Thread: “Request for member”A” in “B” is of non-class type ”C”

  1. #1
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry “Request for member”A” in “B” is of non-class type ”C”

    Hi all,

    can someone explain why i get the error “Request for member”A” in “B” is of non-class type ”C” if i use a constructor without parameters C B(); and why this error dissappear if i use C B(1); (With 1 as dummy param which is not used anywhere else)

    By the way the appropriate header file is always included and the code for the constructors are identical.

    Thanks
    dexli

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: “Request for member”A” in “B” is of non-class type ”C”

    For constructor without parameter use:
    Qt Code:
    1. CLASS_NAME variableName; //without ()
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: “Request for member”A” in “B” is of non-class type ”C”

    Show the code, which compiler is used ?
    This compiles without complains with g++ 4.5.2
    Qt Code:
    1. class Test{
    2. public:
    3. Test(){
    4. }
    5. Test(int){
    6. }
    7. };
    8.  
    9. int main(){
    10. Test t;
    11. Test t1();
    12. Test t2(1);
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: “Request for member”A” in “B” is of non-class type ”C”

    @stampede, it compiles, but if you modify the code to "track" the constructors calls
    Qt Code:
    1. #include <iostream>
    2. class Test{
    3. public:
    4. Test(){
    5. std::cout << "Test()\n";
    6. }
    7. Test(int){
    8. std::cout << "Test(int)\n";
    9. }
    10. };
    11.  
    12. int main(){
    13. Test t;
    14. Test t1();
    15. Test t2(1);
    16. std::cin.get();
    17. return 0;
    18. }
    To copy to clipboard, switch view to plain text mode 
    You will see that only one Test() and one Test(int), that is because Test t1(); is "compiled" as a function declaration (a function called t1 that returns a Test and takes no parameter)
    //i can't test right now, but as far as i know this is the standard C++ behavior.

  5. #5
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: “Request for member”A” in “B” is of non-class type ”C”

    Hi all,

    thanks for the reply but it seems that i do not make clear enough what's the problem.


    Qt Code:
    1. class Test{
    2.  
    3. public:
    4. unsigned int x;
    5. Test(){
    6. x = 1;
    7. }
    8.  
    9. Test(unsigned int y)
    10. {
    11. x = 1;
    12. }
    13.  
    14. }
    15.  
    16.  
    17. int main(){
    18. Test t1();
    19. Test t2(1);
    20. unsigned int c =t2.x; // no problem
    21. unsigned int d =t1.x; // compiler error
    22. return 0;
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: “Request for member”A” in “B” is of non-class type ”C”

    @Zlatomir: you are right, tested.
    Test t1(); is the same as Test t1(void);, so its a declaration indeed.
    It's like that when you use stack, you can use new Test(); and new Test; if allocating with operator new.
    @up: yes, because t1 is function (declaration)
    Last edited by stampede; 21st April 2011 at 10:35. Reason: updated contents

  7. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: “Request for member”A” in “B” is of non-class type ”C”

    That error is because t1 declared: Test t1(); is not an Test instance.
    If you use Test t1; //without () it would work.

    LE: stampede was faster
    The empty parentheses for default constructor can be used when you create objects on the heap, so:
    Qt Code:
    1. Test *ptr = new Test(); //is ok
    2. //so is this
    3. Test *ptr = new Test; //ok too
    To copy to clipboard, switch view to plain text mode 
    Isn't C++ a beautiful language?

  8. #8
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: “Request for member”A” in “B” is of non-class type ”C”

    Yeah, it's a quirk of the language. Because of the (weird) order that C/C++ evaluates and assigns type values, the "Test t1();" statement is interpreted as a call, not a declaration.

    It's just one of those "gotchas" that one learns of (sometimes several times) over the years.

  9. #9
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: “Request for member”A” in “B” is of non-class type ”C”

    @DanH: trying to call a function without definition will trigger a linker error about some unresolved external, so the is interpreted by the compiler as a function declaration, not as a function call.

  10. #10
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: “Request for member”A” in “B” is of non-class type ”C”

    Whatever -- you don't get what you want, and usually get a compile error.

  11. #11
    Join Date
    Mar 2011
    Posts
    22
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: “Request for member”A” in “B” is of non-class type ”C”

    Thanks for the hint without the braces after the call.

Similar Threads

  1. Replies: 7
    Last Post: 19th April 2011, 12:20
  2. 'Class' does not name a type error
    By naturalpsychic in forum Qt Programming
    Replies: 9
    Last Post: 1st February 2011, 15:43
  3. Replies: 12
    Last Post: 28th May 2010, 00:07
  4. Determine Class Type of QObject Parent
    By photo_tom in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2010, 17:42
  5. Replies: 2
    Last Post: 22nd December 2009, 20:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.